package wpmp.security.client.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import wcommons.lang.UrlUtils;
import wpmp.security.client.internal.PmpButtonConfigUtils;
import wpmp.security.client.internal.PmpResourceUtils;
import wpmp.utils.quickbean.ButtonConfig;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 6:59:24 PM Dec 24, 2012
 */
public class ButtonConfigUtils {

	/**
	 * @see #hidden(HttpServletRequest, HttpServletResponse, ButtonConfig)
	 */
	public static boolean hidden(HttpServletRequest req, HttpServletResponse resp, String key) {
		return hidden(req, resp, PmpButtonConfigUtils.get(key));
	}

	/**
	 * <pre>
	 * 判断按钮是否隐藏
	 * 1.如果没有配置按钮，则显示
	 * 2.如果当前用户是管理员，则显示
	 * 3.如果按钮状态是disabled则隐藏
	 * 4.如果当前用户没有权限操作，则隐藏
	 * </pre>
	 * 
	 * @param req
	 * @param resp
	 * @param entity
	 * @return
	 */
	static boolean hidden(HttpServletRequest req, HttpServletResponse resp, ButtonConfig entity) {
		// 没有找到配置，默认隐藏
		if (entity == null) {
			return false;
		}
		if (UserUtils.isAdmin(req)) {
			return false;
		}
		if (entity.isDisabled()) {
			return true;
		}
		return PmpResourceUtils.checkPermission(req, resp,
				UrlUtils.spliceParams(entity.getBaseUrl(), entity.getParaUrl())) != 200;
	}
}
